package at.ac.tuwien.swa.sparrow.common

import com.google.common.net.HostAndPort
import java.util.UUID
import java.util.concurrent.atomic.AtomicLong
import org.codehaus.jackson.annotate.JsonIgnore
import scala.annotation.meta.{setter, getter, field}

trait PersistentObject extends Serializable with Equals {
	var id: Long = _
}

case class Account(var username: String, @(JsonIgnore @field @getter @setter) private[sparrow] val password: String, credits: AtomicLong) extends PersistentObject {
	def passwordMatches(password: String) = this.password == password

	override def toString = username

	override def equals(obj: Any) = if (canEqual(obj)) username == obj.asInstanceOf[this.type].username else false
}

case class Client(var account: Account) extends PersistentObject {
	override def toString = account.toString
}

case class Peer(peerId: UUID, account: Account, var address: HostAndPort, var lastOnline: Long, var online: Boolean) extends PersistentObject {

	def update(address: HostAndPort = this.address, online: Boolean = true, lastOnline: Long = System.currentTimeMillis) = {
		this.address = address
		this.lastOnline = lastOnline
		this.online = online
		this
	}

	override def toString = s"$account [$address]"

	override def equals(obj: Any) = if (canEqual(obj)) {
		val other = obj.asInstanceOf[Peer]
		account == other.account && address == other.address
	} else false
}

case class TrackMetadata(title: String, artist: String, album: String) extends PersistentObject {
	def this(info: TrackInfo) = this(info.title, info.artist, info.album)

	override def toString: String = s"$artist - $title"
}

case class SongRecognition(metadata: TrackMetadata, time: Long = System.currentTimeMillis) extends PersistentObject {
}
